<template>
    <div class="goodsInfo-container">

        <!-- 
            小球动画优化思路：
                1.分析导致动画不准确的本质：把小球的位置写死了 局限到了某一分辨率下 滚动条未滚动的情况下
                2.因此不能讲横向纵向距离写死，应该根据不同情况动态计算
                3.解决思路： 得到徽标的横纵坐标 在得到计数器的横纵坐标 求差得到要移动的距离
                4. 使用 domObject.getBoundingClientRect() 可以得到元素的横纵坐标
         -->
        <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>

        <!-- 商品轮播图 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                     <swiper :swipeList="photoImgs" :isFull="false"></swiper>
                </div>
            </div>
        </div>

        <!-- 商品购买 -->
        <div class="mui-card">
            <div class="mui-card-header">{{ goodsInfo.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:<del>￥{{ goodsInfo.marketPrice }}</del>&nbsp;&nbsp;销售价:<span class="now-price">￥{{ goodsInfo.sellPrice }}</span>
                    </p>
                    <p>
                        购买数量:<numbox @getCount="getSelectedCount" :maxNum="goodsInfo.stockQuantity"></numbox>
                    </p>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addShopCar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>

        <!-- 商品参数 -->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号:{{ goodsInfo.goodsNo }}</p>
                    <p>库存情况:{{ goodsInfo.stockQuantity }}</p>
                    <p>上架时间:{{ goodsInfo.addTime | dateFormat }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" plain size="large" @click="goDesc(goodsInfo.id)">图文介绍</mt-button>
                <mt-button type="danger" plain size="large" @click="goComment(goodsInfo.id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
//导入mui中Toast组件
import { Toast } from 'mint-ui';
//导入轮播图组件
import swiper from '../subcomponent/Swiper.vue'
//导入数字选择框组件
import numbox from '../goods/GoodsInfo_Numbox.vue'
export default {
    data() {
        return {
            id: this.$route.params.id,
            photoImgs: [],
            goodsInfo: {},
            selectedCount: 1,   //保存用户选择的商品数量，默认为1
            ballFlag: false //控制小球是否显示的标识符
        }
    },
    methods: {
        getPhotoImgs() {
            this.$http.get('vue/getphotoImgs/' + this.id).then(result => {
                if (result.body.status === 0) {
                    
                    this.photoImgs = result.body.message
                    
                } else {
                    Toast('获取商品图片失败')
                }
            });
        },
        getGoodsInfo() {
            this.$http.get('vue/getGoodsInfo/' + this.id).then(result => {
                if (result.body.status === 0) {
                    
                    this.goodsInfo = result.body.message
                    
                } else {
                    Toast('获取商品详细信息失败')
                }
            });
        },
        goDesc(id) {
            this.$router.push('/home/goodsDesc/' + id)
        },
        goComment(id) {
            this.$router.push('/home/goodsComment/' + id)
        },
        addShopCar() {
            //添加到购物车
            this.ballFlag = !this.ballFlag
            //创建一个要保存到store中的商品对象
            var goods = {
                id: this.id,
                count: this.selectedCount,
                price: this.goodsInfo.sellPrice,
                selected: true
            }

            //调用mutations中的addGoodsToCar保存到购物车
            this.$store.commit('addGoodsToCar',goods)


        },
        //用于获取子组件传递的值
        getSelectedCount(count) {
            //当子组件把选中的数量传递给父组件的时候，把选中的值保存到data中的selectedCount上
            this.selectedCount = count
        },
        //小球半场动画
        beforeEnter(el) {
            el.style.transform = "translate(0, 0)"
        },
        enter(el,done) {
            el.offsetWidth
            //获取小球位置
            const ballPosition = this.$refs.ball.getBoundingClientRect()
            //获取徽标位置
            const badgePosition = document.getElementById('badge').getBoundingClientRect()

            const xDist = badgePosition.left - ballPosition.left
            const yDist = badgePosition.top - ballPosition.top

            el.style.transform = 'translate('+xDist+'px, '+yDist+'px)'
            el.style.transition = 'all 0.5s cubic-bezier(.4,-0.3,1,.68)'
            done()
        },
        afterEnter(el) {
            this.ballFlag = ! this.ballFlag
        }
    },
    components: {
        swiper,
        numbox
    },
    created() {
        this.getPhotoImgs()
        this.getGoodsInfo()
    }
}
</script>

<style lang="scss" scoped>
    .goodsInfo-container {
        background-color: #eee;
        // 取消塌陷
        overflow: hidden;

        .ball {
            width: 15px;
            height: 15px;
            border-radius: 50%;
            background-color: red;
            position: absolute;
            z-index: 99;
            top: 430px;
            left: 138px;

        }

        // 此时应该高度100% 宽度自适应
        .mint-swipe {
            height: 200px;
            
            .mint-swipe-item {
                width: 100%;
                height: 100%;
            }
        }

        .price {
            .now-price {
                color: red;
                font-size: 16px;
                font-weight: bold;
            }
        }

        .mui-card-footer {
            display: block;
            button {
                margin: 15px 0;
            }
        }
    }
</style>